#pragma once

#include<iostream>
#include<string.h>
#include<assert.h>
using namespace std;

namespace byh
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		// constructor + destructor function 
		vector() {}

		// vector v2(v1)
		vector(const vector<T>& v)
		{
			reserve(v.capacity());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(int n, const T& val = T())
		{
			reserve(n);
			for (int i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(const initializer_list<T>& il)
		{
			reserve(il.size());
			for (auto& e : il)
			{
				push_back(e);
			}
		}

		template<class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			reserve(last - first);
			while (first != last)
			{
				push_back(*first);
				first++;
			}
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		// assignment operator overloading
		// v2 = v1
		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		// iterators
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}

		const_iterator begin() const
		{
			return _start;
		}

		const_iterator end() const
		{
			return _finish;
		}

		// capacity
		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		bool empty() const
		{
			return _start == _finish;
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t len = size();

				T* temp = new T[n];
				//memcpy(temp, _start, size() * sizeof(T));
				for (int i = 0; i < len; i++)
				{
					temp[i] = _start[i];
				}
				delete[] _start;
				_start = temp;
				_finish = temp + len;
				_end_of_storage = temp + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n < size())
				_finish = _start + n;
			else
			{
				for (int i = size(); i < n; i++)
				{
					push_back(val);
				}
			}
		}

		// element access
		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		// modify
		void push_back(const T& val)
		{
			if (_finish == _end_of_storage)
				reserve(capacity() == 0 ? 4 : capacity() * 2);
			(*_finish) = val;
			_finish++;
		}

		void pop_back()
		{
			assert(_finish > _start);
			_finish--;
		}

		void insert(iterator pos, const T& val)
		{
			assert(pos <= _finish);
			assert(pos >= _start);

			if (_finish == _end_of_storage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + len;
			}
			iterator end = _finish;
			while (end > pos)
			{
				*end = *(end - 1);
				end--;
			}
			*pos = val;
			_finish++;
		}

		void erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);

			while (pos < _finish - 1)
			{
				*pos = *(pos + 1);
				pos++;
			}
			_finish--;
		}

		void swap(vector<T>& v)
		{
			std::swap(v._start);
			std::swap(v._finish);
			std::swap(v._end_of_storage);
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;
	};

	template<class T>
	void print_vector(const vector<T>& v)
	{
		//for (int i = 0; i < v.size(); i++)
		//{
		//	cout << v[i] << " ";
		//}
		//cout << endl;

		typename vector<T>::const_iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;

		//for (auto& e : v)
		//{
		//	cout << e << " ";
		//}
		//cout << endl;
	}

	void Test_vector()
	{
		//vector<int> v;
		//v.push_back(1);
		//v.push_back(2);
		//v.push_back(3);
		//v.push_back(4);
		//v.push_back(5);
		//print_vector(v);

		//vector<int> v1;
		//v1.push_back(1);
		//v1.push_back(2);
		//v1.push_back(3);
		//v1.push_back(4);
		//v1.push_back(5);
		//vector<int> v2(v1);
		//print_vector(v2);

		//vector<int> v(10);
		//print_vector(v);

		//vector<int> v = { 1,2,3,4,5 };
		//vector<int> v({ 1,2,3,4,5 });
		//print_vector(v);

		//vector<int> v1(10, 1);
		//std::string s("hello");
		//vector<int> v(v1.begin(), v1.end());
		//print_vector(v);

		//vector<int> v(10, 1);
		//v.pop_back();
		//print_vector(v);

		//vector<int> v;
		//v.insert(v.begin(), 1);
		//v.insert(v.begin(), 2);
		//v.insert(v.begin(), 3);
		//v.insert(v.begin(), 4);
		//v.insert(v.end(), 5);
		//v.erase(v.end() - 1);
		//print_vector(v);
		//v.erase(v.begin());
		//v.erase(v.begin());
		//print_vector(v);

		//vector<string> v;
		//v.push_back("1111");
		//v.push_back("2222");
		//v.push_back("3333");
		//v.push_back("4444");
		//v.push_back("5555");
		//print_vector(v);
		
		//vector<int> v;
		//v.push_back(1);
		//v.push_back(2);
		//v.push_back(3);
		//v.push_back(4);
		//vector<int>::iterator it = v.begin();
		//cout << *it << endl;
		//v.insert(v.begin(), 10);
		//cout << *it << endl;

		vector<int> v;
		v.push_back(1);
		v.push_back(2);
		v.push_back(2);
		v.push_back(3);
		v.push_back(6);
		vector<int>::iterator it = v.begin();
		while (it != v.end())
		{
			if (*it % 2 == 0)
				v.erase(it);
			else
				it++;
		}
		print_vector(v);
	}
}